/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import prompt from '@system.prompt';
import state from '../../../../js/share/js/login/state.js';
import faUtils from '../../../share/js/fa-utils.js';
import CommonUtils from '../../../../js/share/js/common-utils.js';
import router from '@system.router';
import HiLinkDevice from '../../../share/js/hilink-device.js';
import DataType from '../../../share/js/data-type.js';
import commonUtils from '../../../../js/share/js/common-utils.js'

const JS_TAG = 'JS/Page/Index: ';

export default {
    data: {
        protocolAgreed: false,
        deviceConnected: false,
        state: state.connection.PRIVACY,
        timeOut: 0
    },
    ////////////////////////////////////////
    ////////        页面初始化       ////////
    onInit() {
        console.debug(JS_TAG + 'Page init started');
        this.state=state.connection.PRIVACY;
    },
    onShow(){
        console.log(JS_TAG+'state'+this.state);
    },
    onStartLogin() {
        this.changeState(state.connection.CONNECTING)
    },

    // 查看隐私协议
    privacyButtonClick() {
        console.debug(JS_TAG + 'onPrivacyButtonClick');
        router.push({
            uri: 'pages/privacy/privacy'
        })
    },

    // 登录成功回调
    onLoginSuccess(resultObj) {
//        console.debug(JS_TAG + 'Login success:' + JSON.stringify(resultObj));
//        this.onLoginFinished();
        this.toControlPage();
    },

    // 登录失败回调
    onLoginError(loginError) {
        console.error(JS_TAG + 'onLoginError, error = ' + JSON.stringify(loginError));
        this.changeState(state.connection.PRIVACY);
    },

    // 登录操作结束
    async onLoginFinished() {
        faUtils.logInfo(`${JS_TAG}  onLoginFinished: deviceId: ${this.deviceId} uuid:${this.uuid} ProtocolID: ${this.ProtocolID}`)
        if (this.deviceId == undefined) {
            // 需要代理注册
            faUtils.logInfo(`${JS_TAG}  start proxyRegisterRequired`)
            CommonUtils.requireProxyRegister()
        } else {
            // 连接设备
            // this.connectToDevice();
            this.toControlPage()
        }
    },
    connectToDevice() {
        console.debug(JS_TAG + 'connectToDevice')
        // this.changeState(state.connection.CONNECTING);
        HiLinkDevice.connect((res) => {
            this.timeOut = Date.now();
        })
    },

    reConnect() {
        console.debug(JS_TAG + 'Reconnect!')
        this.connectToDevice();
    },
    onBackPress() {
        console.debug(JS_TAG + 'On back pressed.');
    },
    updateStateToConnectFailed() {
        this.changeState(state.connection.CONNECT_FAILED);
    },
    toControlPage() {
        console.debug(JS_TAG + 'Connected, jumping to control.');
        CommonUtils.jumpControlPage();
    },
    changeState(newState, ...oldStates) {
        // 使用此函数以在状态之间进行转换，对应状态将显示对应的页面。
        console.debug('Trying change state from: ' + oldStates + ' to: ' + newState);
        if (oldStates == undefined || oldStates.length == 0 || oldStates.indexOf(this.state) > -1) {
            console.debug('State changed from: ' + this.state + ' to: ' + newState);
            this.state = newState;
        } else {
            console.error('Can not change from: ' + this.state + ' to: ' + newState);
        }
    }
}
